//
//  BellStatisViewController.swift
//  BellManage
//
//  Created by Bestsch on 16/3/29.
//  Copyright © 2016年 wsl. All rights reserved.
//

import UIKit

class BellStatisViewController: BaseViewController,MBProgressHUDDelegate{
    
    var segmentedControl: UISegmentedControl!
    
    var teacherLabel,familyLabel,PVLabel,UVLabel,allUserLabel,nowUserLabel: UILabel!
    
    var teacherCountLabel,familyCountLabel,nowUserCountLabel,todayLabel, totleLabel: UILabel!
    
    var teacher: String!,family: String!,PV: String!,UV: String!
    var view1,view2,view3,view4,view5,view6,view7,view8, headerView: UIView!
    var tableView: UITableView!
    
    var hud: MBProgressHUD!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // Do any additional setup after loading the view.
        
        self.title = "统计分析"
        
        let array = ["今日访问","本周访问","所有访问"]
        segmentedControl = UISegmentedControl.init(items: array)
        segmentedControl.frame = CGRect(x: 30, y: 74, width: screenW - 60, height: 40)
        segmentedControl.selectedSegmentIndex = 0
        segmentedControl.tintColor = barColor
        
        
        segmentedControl.addTarget(self, action: #selector(BellStatisViewController.selTab), for: UIControlEvents.valueChanged)
        self.view.addSubview(segmentedControl)
        
        InitViews()
        
        getStatis("1")
    }
    func InitViews() {
        
        tableView = UITableView.init(frame: CGRect(x: 0, y: segmentedControl.frame.maxY + 10, width: screenW, height: screenH - segmentedControl.frame.maxY - 30))
        tableView.backgroundColor = UIColor.init(hexString: "f6f6f6", alpha: 1)
        
        tableView.separatorStyle = UITableViewCellSeparatorStyle.none
        self.view.addSubview(tableView)
        
        
        
        
        PVLabel = UILabel.init(frame: CGRect(x: 0 , y: 20, width: screenW, height: 80))
        PVLabel.backgroundColor = UIColor.clear
        PVLabel.numberOfLines = 0
        PVLabel.textAlignment = NSTextAlignment.center
        
        UVLabel = UILabel.init(frame: CGRect(x: 0, y: PVLabel.frame.maxY, width: screenW, height: 80))
        UVLabel.backgroundColor = UIColor.clear
        UVLabel.numberOfLines = 0
        UVLabel.textAlignment = NSTextAlignment.center
        
        allUserLabel = UILabel.init(frame: CGRect(x: 0, y: UVLabel.frame.maxY, width: screenW, height: 80))
        allUserLabel.backgroundColor = UIColor.clear
        allUserLabel.numberOfLines = 0
        allUserLabel.textAlignment = NSTextAlignment.center
        
        // 今日注册数量
        todayLabel = UILabel.init(frame: CGRect(x: 0, y: allUserLabel.frame.maxY + 1, width: screenW, height: 60))
        todayLabel.backgroundColor = UIColor.clear
        todayLabel.textColor = UIColor.darkText
//        todayLabel.text = "今日注册数"
        todayLabel.numberOfLines = 0
        todayLabel.textAlignment = NSTextAlignment.center
        
        view1 = UIView.init(frame: CGRect(x: 0, y: todayLabel.frame.maxY + 1, width: screenW, height: 1))
        view1.backgroundColor = UIColor.init(hexString: "eeeeee", alpha: 1)
        
        teacherLabel = UILabel.init(frame: CGRect(x: 0, y: view1.frame.maxY, width: screenW/2, height: 60))
        teacherLabel.backgroundColor = UIColor.clear
        teacherLabel.textColor = UIColor.darkText
        teacherLabel.numberOfLines = 0
        teacherLabel.textAlignment = NSTextAlignment.center
//        teacherLabel.text = "teacherLabel"
        
        view2 = UIView.init(frame: CGRect(x: screenW/2, y: view1.frame.maxY + 10, width: 1, height: 40))
        view2.backgroundColor = UIColor.init(hexString: "eeeeee", alpha: 1)
        
        familyLabel = UILabel.init(frame: CGRect(x: screenW/2, y: view1.frame.maxY, width: screenW/2, height: 60))
        familyLabel.backgroundColor = UIColor.clear
        familyLabel.textColor = UIColor.darkText
        familyLabel.numberOfLines = 0
        familyLabel.textAlignment = NSTextAlignment.center
//        familyLabel.text = "teacherLabel"
        
//        view4 = UIView.init(frame: CGRect(x: screenW*2/3, y: view1.frame.maxY + 10, width: 1, height: 40))
//        view4.backgroundColor = UIColor.init(hexString: "eeeeee", alpha: 1)
//        
//        nowUserLabel = UILabel.init(frame: CGRect(x: screenW*2/3, y: view1.frame.maxY, width: screenW/3, height: 60))
//        nowUserLabel.backgroundColor = UIColor.clear
//        nowUserLabel.textColor = UIColor.darkText
//        nowUserLabel.numberOfLines = 0
//        nowUserLabel.textAlignment = NSTextAlignment.center
//        nowUserLabel.text = "teacherLabel"
        
        view3 = UIView.init(frame: CGRect(x: 0, y: familyLabel.frame.maxY, width: screenW, height: 1))
        view3.backgroundColor = UIColor.init(hexString: "eeeeee", alpha: 1)
        
        
        // 总共 注册数量
        totleLabel = UILabel.init(frame: CGRect(x: 0, y: view3.frame.maxY, width: screenW, height: 60))
//        totleLabel.text = "总用户数"
        totleLabel.backgroundColor = UIColor.clear
        totleLabel.textColor = UIColor.darkText
        totleLabel.numberOfLines = 0
        totleLabel.textAlignment = NSTextAlignment.center
        
        
        view5 = UIView.init(frame: CGRect(x: 0, y: totleLabel.frame.maxY + 1, width: screenW, height: 1))
        view5.backgroundColor = UIColor.init(hexString: "eeeeee", alpha: 1)
        
        teacherCountLabel = UILabel.init(frame: CGRect(x: 0, y: totleLabel.frame.maxY, width: screenW/2, height: 60))
        teacherCountLabel.backgroundColor = UIColor.clear
        teacherCountLabel.textColor = UIColor.darkText
        teacherCountLabel.numberOfLines = 0
        teacherCountLabel.textAlignment = NSTextAlignment.center
//        teacherCountLabel.text = "teacherCountLabel"
        
        view6 = UIView.init(frame: CGRect(x: screenW/2, y: totleLabel.frame.maxY + 10, width: 1, height: 40))
        view6.backgroundColor = UIColor.init(hexString: "eeeeee", alpha: 1)
        
        familyCountLabel = UILabel.init(frame: CGRect(x: screenW/2, y: totleLabel.frame.maxY, width: screenW/2, height: 60))
        familyCountLabel.backgroundColor = UIColor.clear
        familyCountLabel.textColor = UIColor.darkText
        familyCountLabel.numberOfLines = 0
        familyCountLabel.textAlignment = NSTextAlignment.center
//        familyCountLabel.text = "teacherCountLabel"
        
//        view8 = UIView.init(frame: CGRect(x: screenW*2/3, y: totleLabel.frame.maxY + 10, width: 1, height: 40))
//        view8.backgroundColor = UIColor.init(hexString: "eeeeee", alpha: 1)
//        
//        nowUserCountLabel = UILabel.init(frame: CGRect(x: screenW*2/3, y: totleLabel.frame.maxY, width: screenW/3, height: 60))
//        nowUserCountLabel.backgroundColor = UIColor.clear
//        nowUserCountLabel.textColor = UIColor.darkText
//        nowUserCountLabel.numberOfLines = 0
//        nowUserCountLabel.textAlignment = NSTextAlignment.center
//        nowUserCountLabel.text = "teacherCountLabel"
        
        view7 = UIView.init(frame: CGRect(x: 0, y: teacherCountLabel.frame.maxY, width: screenW, height: 1))
        view7.backgroundColor = UIColor.init(hexString: "eeeeee", alpha: 1)
        
        headerView = UIView.init(frame: CGRect(x: 0, y: 0, width: screenW, height: view7.frame.maxY))
        
        tableView.tableHeaderView = headerView
        
        self.headerView.addSubview(PVLabel)
        self.headerView.addSubview(UVLabel)
        self.headerView.addSubview(allUserLabel)
        
        self.headerView.addSubview(todayLabel)
        self.headerView.addSubview(teacherLabel)
        self.headerView.addSubview(familyLabel)
        
        
        self.headerView.addSubview(totleLabel)
        self.headerView.addSubview(teacherCountLabel)
        self.headerView.addSubview(familyCountLabel)
        
        
        self.headerView.addSubview(view1)
        self.headerView.addSubview(view2)
        self.headerView.addSubview(view3)
        
        self.headerView.addSubview(view5)
        self.headerView.addSubview(view6)
        self.headerView.addSubview(view7)
        
        /*
        self.view.addSubview(PVLabel)
        self.view.addSubview(UVLabel)
        self.view.addSubview(allUserLabel)
        
        self.view.addSubview(todayLabel)
        self.view.addSubview(teacherLabel)
        self.view.addSubview(familyLabel)
//        self.view.addSubview(nowUserLabel)
        
        self.view.addSubview(totleLabel)
        self.view.addSubview(teacherCountLabel)
        self.view.addSubview(familyCountLabel)
//        self.view.addSubview(nowUserCountLabel)
        
        self.view.addSubview(view1)
        self.view.addSubview(view2)
        self.view.addSubview(view3)
//        self.view.addSubview(view4)
        self.view.addSubview(view5)
        self.view.addSubview(view6)
        self.view.addSubview(view7)
//        self.view.addSubview(view8)
         */
        
    }
    func selTab(){
        let n = segmentedControl.selectedSegmentIndex
        switch n {
        case 0:
            getStatis("1")
            break
        case 1:
            getStatis("2")
            break
        case 2:
            getStatis("3")
            break
        default:
            break
        }
    }
    /**
     获取统计
     
     - parameter type: 1今日，2为周，3为所有
     */
    func getStatis(_ type: String!) {
        
        hud = MBProgressHUD.showAdded(to: self.view, animated: true)
        hud.animationType = MBProgressHUDAnimation.fade
        
        if (reachability?.isReachable())! {
            
            let queue = DispatchQueue(label: "queue")
            queue.async {
                let user = getUserPojo()
                let requestXml = "<?xml version=\"1.0\" encoding=\"utf-8\"?><rss version=\"2.0\"><t>4</t><schid>" + user.schid + "</schid><types>" + type + "</types></rss>"
                let url = server + Statistics
                var result = HttpUtils.httpPostUTF(url, with: requestXml)
                result = result?.replacingOccurrences(of: "null", with: "\"\"")
                print(result!)
                DispatchQueue.main.async(execute: {
                    
                    let array = JSON(data: (result?.data(using: String.Encoding.utf8))!)
                    
                    if array.count > 0 {
                        
                        let dic = array
                        
                        let teacherNum = dic["teanum"].stringValue
                        let familyNum = dic["famnum"].stringValue
                        let PVNum = dic["pvnum"].stringValue
                        let UVNum = dic["uvnum"].stringValue
                        let all = dic["allnum"].stringValue
                        var hy = dic["hynum"].stringValue
                        
                        var allteareg = dic["allteareg"].stringValue
                        var allfamreg = dic["allfamreg"].stringValue
                        
                        if allteareg == "" {
                            allteareg = "0"
                        }
                        if allfamreg == "" {
                            allfamreg = "0"
                        }
                        
                        hy = "\(hy)%"
                        let PV = PVNum + "\n访问量"
                        let pv = NSMutableAttributedString.init(string: PV)
                        pv.addAttribute(NSFontAttributeName, value: UIFont.boldSystemFont(ofSize: 45), range: NSMakeRange(0, PVNum.characters.count))
                        pv.addAttribute(NSFontAttributeName, value: UIFont.systemFont(ofSize: 14), range: NSMakeRange(PVNum.characters.count, pv.length - PVNum.characters.count))
                        pv.addAttribute(NSForegroundColorAttributeName, value: barColor, range: NSMakeRange(0, PVNum.characters.count))
                        pv.addAttribute(NSForegroundColorAttributeName, value: UIColor.darkText, range: NSMakeRange(PVNum.characters.count, pv.length - PVNum.characters.count))
                        self.PVLabel.attributedText = pv
                        
                        let UV = UVNum + "\n访问用户"
                        let uv = NSMutableAttributedString.init(string: UV)
                        uv.addAttribute(NSFontAttributeName, value: UIFont.boldSystemFont(ofSize: 40), range: NSMakeRange(0, UVNum.characters.count))
                        uv.addAttribute(NSFontAttributeName, value: UIFont.systemFont(ofSize: 14), range: NSMakeRange(UVNum.characters.count, uv.length - UVNum.characters.count))
                        uv.addAttribute(NSForegroundColorAttributeName, value: barColor, range: NSMakeRange(0, UVNum.characters.count))
                        uv.addAttribute(NSForegroundColorAttributeName, value: UIColor.darkText, range: NSMakeRange(UVNum.characters.count, uv.length - UVNum.characters.count))
                        self.UVLabel.attributedText = uv
                        
                        
                        let ALL = hy + "\n活跃率"
                        let alluser = NSMutableAttributedString.init(string: ALL)
                        alluser.addAttribute(NSFontAttributeName, value: UIFont.boldSystemFont(ofSize: 40), range: NSMakeRange(0, hy.characters.count))
                        alluser.addAttribute(NSFontAttributeName, value: UIFont.systemFont(ofSize: 14), range: NSMakeRange(hy.characters.count, alluser.length - hy.characters.count))
                        alluser.addAttribute(NSForegroundColorAttributeName, value: barColor, range: NSMakeRange(0, hy.characters.count))
                        alluser.addAttribute(NSForegroundColorAttributeName, value: UIColor.darkText, range: NSMakeRange(hy.characters.count, alluser.length - hy.characters.count))
                        self.allUserLabel.attributedText = alluser
                        
                        let Teacher = "教师\n" + teacherNum
                        let teacher = NSMutableAttributedString.init(string: Teacher)
                        teacher.addAttribute(NSFontAttributeName, value: UIFont.systemFont(ofSize: 14), range: NSMakeRange(0, 2))
                        teacher.addAttribute(NSFontAttributeName, value: UIFont.boldSystemFont(ofSize: 30), range: NSMakeRange(2, teacher.length - 2))
                        //                    self.teacherLabel.attributedText = teacher
                        
                        let Family = "家长\n" + familyNum
                        let family = NSMutableAttributedString.init(string: Family)
                        family.addAttribute(NSFontAttributeName, value: UIFont.systemFont(ofSize: 14), range: NSMakeRange(0, 2))
                        family.addAttribute(NSFontAttributeName, value: UIFont.boldSystemFont(ofSize: 30), range: NSMakeRange(2, family.length - 2))
                        //                    self.familyLabel.attributedText = family
                        
                        //                    let now = "总注册用户\n" + all
                        //                    let nowuser = NSMutableAttributedString.init(string: now)
                        //                    nowuser.addAttribute(NSFontAttributeName, value: UIFont.systemFont(ofSize: 14), range: NSMakeRange(0, 5))
                        //                    nowuser.addAttribute(NSFontAttributeName, value: UIFont.boldSystemFont(ofSize: 30), range: NSMakeRange(5, nowuser.length - 5))
                        //                    self.nowUserLabel.attributedText = nowuser
                        
                        
                        // 本周、注册数量
                        let totleTeacher = "教师\n" + allteareg
                        let countTeacher = NSMutableAttributedString.init(string: totleTeacher)
                        countTeacher.addAttribute(NSFontAttributeName, value: UIFont.systemFont(ofSize: 14), range: NSMakeRange(0, 2))
                        countTeacher.addAttribute(NSFontAttributeName, value: UIFont.boldSystemFont(ofSize: 30), range: NSMakeRange(2, countTeacher.length - 2))
                        
                        
                        let totleFamily = "家长\n" + allfamreg
                        let countFamily = NSMutableAttributedString.init(string: totleFamily)
                        countFamily.addAttribute(NSFontAttributeName, value: UIFont.systemFont(ofSize: 14), range: NSMakeRange(0, 2))
                        countFamily.addAttribute(NSFontAttributeName, value: UIFont.boldSystemFont(ofSize: 30), range: NSMakeRange(2, countFamily.length - 2))
                        self.familyCountLabel.attributedText = countFamily
                        
                        
                        if self.segmentedControl.selectedSegmentIndex == 0 {
                            self.todayLabel.text = "今日注册数"
                        }else if self.segmentedControl.selectedSegmentIndex == 1 {
                            self.todayLabel.text = "本周注册数"
                        }else if self.segmentedControl.selectedSegmentIndex == 2 {
                            self.todayLabel.text = "总用户数"
                        }
                        
                        
                        self.todayLabel.font = UIFont.boldFlatFont(ofSize: 20.0)
                        
                        
                        self.totleLabel.font = UIFont.boldFlatFont(ofSize: 20.0)
                        
                        if type == "1" || type == "2" {
                            
                            self.teacherLabel.attributedText = teacher
                            self.familyLabel.attributedText = family
                            
                            self.totleLabel.text = "总用户数"
                            self.teacherCountLabel.attributedText = countTeacher
                            self.familyCountLabel.attributedText = countFamily
                            self.view5.isHidden = false
                            self.view6.isHidden = false
                            self.view7.isHidden = false
                            
                            
                        }else {
                            
                            self.teacherLabel.attributedText = countTeacher
                            self.familyLabel.attributedText = countFamily
                            
                            self.totleLabel.text = ""
                            self.teacherCountLabel.text = ""
                            self.familyCountLabel.text = ""
                            self.view5.isHidden = true
                            self.view6.isHidden = true
                            self.view7.isHidden = true
                            
                        }
                        //                    if type == "1" || type == "2" {
                        //
                        //                        self.view1.isHidden = true
                        //                        self.familyLabel.isHidden = true
                        //                        self.view2.isHidden = true
                        //                        self.teacherLabel.isHidden = true
                        //                        self.view3.isHidden = true
                        //                        self.view4.isHidden = true
                        //                        self.nowUserLabel.isHidden = true
                        //                    }else{
                        //                        
                        //                        self.view1.isHidden = false
                        //                        self.familyLabel.isHidden = false
                        //                        self.view2.isHidden = false
                        //                        self.teacherLabel.isHidden = false
                        //                        self.view3.isHidden = false
                        //                        self.view4.isHidden = false
                        //                        self.nowUserLabel.isHidden = false
                        //                    }
                        
                        
                        
                        
                        
                        self.hud.isHidden = true
                    }else {
                        self.hud.isHidden = true
                    }
                    
                    self.tableView.reloadData()
                })
            }
            
        } else {
            HYLog("网络连接：不可用")
            self.hud.isHidden = true
        }
        
        

        
    }
    
    
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    
    /*
     // MARK: - Navigation
     
     // In a storyboard-based application, you will often want to do a little preparation before navigation
     override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
     // Get the new view controller using segue.destinationViewController.
     // Pass the selected object to the new view controller.
     }
     */
    
}
